import {
	baseUrl
} from '@/utils/request.js';

/**
 * @function 时间戳转换
 */
export function timestampToTime(timestamp) {
	if (timestamp == 0) return
	var date = new Date(timestamp);
	var Y = date.getFullYear() + "/";
	var M =
		(date.getMonth() + 1 < 10 ?
			"0" + (date.getMonth() + 1) :
			date.getMonth() + 1) + "/";
	var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
	var h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
	var m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ":";
	var s = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()) + "";
	return Y + M + D + h + m + s
}

/** 
 * @function 获取当前地理位置
 */
export function getLocation() {
	uni.getSetting({
		success: (res) => {
			if (!res.authSetting['scope.userLocation']) {
				uni.authorize({
					scope: 'scope.userLocation',
					success: () => {
						uni.getLocation({
							type: 'wgs84',
							altitude: true,
							success: (results) => {								
								uni.setStorageSync('user_site',JSON.stringify(results))
							}
						})
					},
					fail: (err) => {
						uni.showModal({
							title: '授权提示',
							content: '该应用需要使用你的位置权限，是否同意？',
							success: function(res) {
								if (res.confirm) {
									uni.openSetting();
								} else if (res.cancel) {}
							}
						})
					}
				})
			} else {
				uni.getLocation({
					type: 'wgs84',
					altitude: true,
					success: (results) => {								
						uni.setStorageSync('user_site',JSON.stringify(results))
					}
				})
			}
		}
	})
}
/** 
 * @function 获取当前时间段
 */
export function traversalTime(timestamp) {
	if (timestamp == 0) return
	var date = new Date(timestamp);
	var h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
	var m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
	var hours = date.getHours();
	var state = '';
	if (hours >= 0 && hours <= 10) {
		state = "早上 ";
	} else if (hours > 10 && hours <= 14) {
		state = "中午 ";
	} else if (hours > 14 && hours <= 18) {
		state = "下午 ";
	} else if (hours > 18 && hours <= 24) {
		state = "晚上 ";
	}

	return state + h + ':' + m
}

/** 
 * @function 计算时间为刚刚、几分钟前、几小时前、几天前··
 */
export function timeago(timestamp) {
	var minute = 1000 * 60; //把分，时，天，周，半个月，一个月用毫秒表示
	var hour = minute * 60;
	var day = hour * 24;
	var week = day * 7;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime(); //获取当前时间毫秒
	var diffValue = now - timestamp; //时间差
	var result = ""; // 返回值

	if (diffValue < 0) {
		return;
	}
	var minC = diffValue / minute; //计算时间差的分，时，天，周，月
	var hourC = diffValue / hour;
	var dayC = diffValue / day;
	var weekC = diffValue / week;
	var monthC = diffValue / month;
	if (monthC >= 1 && monthC <= 3) {
		result = " " + parseInt(monthC) + "月前"
	} else if (weekC >= 1 && weekC <= 3) {
		result = " " + parseInt(weekC) + "周前"
	} else if (dayC >= 1 && dayC <= 6) {
		result = " " + parseInt(dayC) + "天前"
	} else if (hourC >= 1 && hourC <= 23) {
		result = " " + parseInt(hourC) + "小时前"
	} else if (minC >= 1 && minC <= 59) {
		result = " " + parseInt(minC) + "分钟前"
	} else if (diffValue >= 0 && diffValue <= minute) {
		result = "刚刚"
	} else {
		var datetime = new Date();
		datetime.setTime(timestamp);
		var Nyear = datetime.getFullYear();
		var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
		var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
		var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
		var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
		var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
		result = Nyear + "-" + Nmonth + "-" + Ndate
	}
	return result;
}
/** 
 * @function 上传图片
 */
export function uploadFile(filePath) {
	// 添加token
	let token = uni.getStorageSync('token')
	let options = {
		url: baseUrl + '/uploadMultiple/albumAvatar'
	}
	return new Promise((resolved, rejected) => {
		uni.uploadFile({
			url: options.url,
			filePath: filePath,
			name: 'avatar',
			header: {
				'content-type': 'multipart/form-data',
				'token': token
			},
			// formData: optionsData,
			success: (res) => {
				if (res.statusCode == 200) {
					resolved(res)
				} else {
					rejected(res)
				}
			},
			fail: (err) => {
				rejected(err)
			}
		})
	})
}

/** 
 * @function 打字效果
 */
export function typingEffect(textWriter) {
	let i = 0;
	var teststr = "";
	let timer = setInterval(() => {
		if (i <= textWriter.length) {
			teststr = textWriter.slice(0, i++) + '_'
			console.log(teststr,'56998')
			return teststr
		} else {
			teststr = textWriter
			clearInterval(timer);
			console.log(teststr,'返回相同')
			return teststr
		}
	}, 30)
	return teststr
}
